<template>
  <Form autocomplete="off" class="user-form" ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110">
    <FormItem v-if="isAdd" label="上级单位" prop="orgId">
      <TreeSelect filterable width="100%" :data="orgList"  v-model="formValidate.orgId" placeholder="请选择上级单位"></TreeSelect>
    </FormItem>
    <FormItem label="单位名称" prop="name" >
      <Input :disabled="!isAdd"  name="name"  v-model="formValidate.name" placeholder="请输入单位名称"></Input>
    </FormItem>
    <FormItem  label="单位简称" prop="shortName" >
      <Input  name="shortName"  v-model="formValidate.shortName" placeholder="请输入单位名称"></Input>
    </FormItem>
    <FormItem v-if="isAdd"  label="单位类型" prop="type" >
      <RadioGroup name="type" v-model="formValidate.type">
        <Radio v-for="item in unitTypeList" :label="item.code" >{{item.name}}</Radio>
      </RadioGroup>
    </FormItem>
    <FormItem label="单位地址" prop="address">
      <Input v-model="formValidate.address"   placeholder="请输入单位地址"></Input>
    </FormItem>
    <FormItem label="注册日期" prop="regdate">
      <DatePicker v-model="formValidate.regdate" type="date" placeholder="请选择时间" ></DatePicker>
    </FormItem>
    <FormItem label="城市区域" prop="regionCode">
      <Cascader change-on-select filterable width="100%" :data="list"  v-model="formValidate.regionCode" placeholder="请选择城市区域"></Cascader>
    </FormItem>
    <FormItem v-if="isAdd" label="管理员姓名" prop="adminName">
      <Input v-model="formValidate.adminName"   placeholder="请输入管理员姓名"></Input>
    </FormItem>
    <FormItem v-if="isAdd" label="管理员手机号" prop="adminPhone">
      <Input v-model="formValidate.adminPhone"   placeholder="请输入管理员手机号"></Input>
    </FormItem>
    <FormItem v-if="isAdd" label="管理员密码" prop="adminPwd">
      <Input v-model="formValidate.adminPwd"   placeholder="请输入管理员密码"></Input>
    </FormItem>
    <div class="ivu-text-center mt20">
      <Button  @click="handleSubmit('formValidate')">提交</Button>
    </div>
  </Form>
</template>
<script>
import {companyAdd,companyUpdate} from '@/api/userManage';
import {validatorTel} from '@/libs/validator';
export default {
  created() {

  },
  props:{
    list:{
      type:Array,
      default:()=>{
        return []
      }
    },
    unitTypeList:{
      type:Array,
      default:()=>{
        return []
      }
    },
    orgList:{
      type:Array,
      default:()=>{
        return []
      }
    },
    isAdd:{
      type:Boolean,
      default:true,
    },
    editorData:{
      type:Object,
      default:()=>{
        return {};
      }
    },
  },
  watch:{
    isAdd(newdata,oldData){
        this.init();
    }
  },
  mounted() {
    this.init()
  },
  data () {
    return {
      pwdType:'text',
      formValidate: {
        orgId:'',
        name:'',
        shortName:'',
        type:'',
        address:'',
        regdate:'',
        regionCode:[],
        adminName:'',
        adminPhone:'',
        adminPwd:'',
        id:'',
      },
      ruleValidate: {
        orgId: [
          { required: true,  message: '单位所属单位不能为空', trigger: 'change' },
        ],
        name: [
          { required: true, message: '单位名称不能为空', trigger: 'blur' },
          { max: 100, message: '单位名称长度不能超过100个字符', trigger: 'blur' },
        ],
        shortName: [
          { required: true, message: '单位简称不能为空', trigger: 'blur' },
          { max:100, message: '单位简称不能超过100个字符', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '单位类型必须选择', trigger: 'change' },
        ],
        address: [
          { required: true, message: '单位地址不能为空', trigger: 'blur' },
          { max: 100, message: '单位地址长度不能超过100个字符', trigger: 'blur' },
        ],
        adminName: [
          { required: true, message: '管理员姓名不能为空', trigger: 'blur' },
          { max:50, message: '管理员姓名不能超过50个字符', trigger: 'blur' },
        ],
        adminPhone: [
          { required: true,  message: '管理员手机号不能为空', trigger: 'blur' },
          { validator: validatorTel, trigger: 'blur' }

        ],
        adminPwd: [
          { required: true, message: '单位密码不能为空', trigger: 'blur' },
          { max:12, message: '管理员手机号不能超过12个字符', trigger: 'blur' },
        ],

      }
    }
  },
  methods: {
    init(){
      this.formValidate=this.isAdd?{
        orgId:'',
        name:'',
        shortName:'',
        type:'',
        address:'',
        regdate:'',
        regionCode:[],
        adminName:'',
        adminPhone:'',
        adminPwd:'',
        id:'',
      }:{...this.editorData}
    },
    getSumbitParm(){
      return {
        ...this.formValidate,
        province :_.get(this.formValidate,'regionCode.0')||'',
        city:_.get(this.formValidate,'regionCode.1')||'',
        district:_.get(this.formValidate,'regionCode.2')||'',
      }
    },
    async Add(){
      return  await companyAdd(this.getSumbitParm());
    },
    async updata(){
      return await companyUpdate(this.getSumbitParm())
    },
    handleSubmit (name) {
      this.$refs[name].validate(async (valid) => {
        if (valid) {
        let res = this.isAdd? await this.Add():await this.updata();
          if(_.get(res,'code')==0){
            this.$emit('success');
            if(this.isAdd){
              this.$Message.success({
                            duration:1.5,
                            content:'新增成功',
                            onClose:function(){
                                window.location.reload();
                            }
                        })
            }else{
              this.$Message.success('修改成功');
            }
          }else{
            this.$Message.error(_.get(res,'message'));
          }

        } else {
          // this.$Message.error('Fail!');
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields();
    }
  }
}
</script>
<style scoped lang="less">

</style>
